package com.gopay.common.manager.ptbank.impl;

import com.gopay.common.dao.ptbank.GsrtBankHfTdFundDtlDao;
import com.gopay.common.domain.ptbank.GsrtBankHfTdFundDtl;
import com.gopay.common.manager.ptbank.GsrtBankHfTdFundDtlManager;
import com.gopay.common.order.dao.CommonQueryDAO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 资金变动明细文件
 * GsrtBankHfTdFundDtlManagerImpl.java
 * 2016年4月27日
 * @author donghuijun
 *  www.gopay.com.cn Inc.All rights reserved.
 */
@Service("gsrtBankHfTdFundDtlManager")
public class GsrtBankHfTdFundDtlManagerImpl implements GsrtBankHfTdFundDtlManager {

	@Resource(name = "gsrtBankHfTdFundDtlDao")
    private GsrtBankHfTdFundDtlDao gsrtBankHfTdFundDtlDao;

    @Resource(name = "commonQueryDAO")
    private CommonQueryDAO commonQueryDAO;

    /**
     * 查询某天的数据
     * @param date 8位字符串，代表某天
     * @return 恒丰
     */
    @Override
    public List<GsrtBankHfTdFundDtl> getDtlListByDate(String date) {
        List<Object[]> list = commonQueryDAO.findBySql(getFundDtlSql(date));
        List<GsrtBankHfTdFundDtl> fundDtlList = new ArrayList<>();

        for (Object[] objects : list) {
            if(objects[2] != null || objects[3] != null ) {
                BigDecimal chargeSum = new BigDecimal(objects[2] == null ? "0.00" : objects[2].toString());
                BigDecimal drawSum = new BigDecimal(objects[3] == null ? "0.00" : objects[3].toString());
                GsrtBankHfTdFundDtl gsrtBankHfTdFundDtl = new GsrtBankHfTdFundDtl();
                gsrtBankHfTdFundDtl.setTransdate(date);
                gsrtBankHfTdFundDtl.setPlatid(objects[1] == null ? "" : objects[1].toString());
                gsrtBankHfTdFundDtl.setMercustid(objects[0] == null ? "" : objects[0].toString());
                gsrtBankHfTdFundDtl.setCashc(chargeSum);
                gsrtBankHfTdFundDtl.setCashd(drawSum);
                gsrtBankHfTdFundDtl.setOffsetbal(chargeSum.subtract(drawSum));
                gsrtBankHfTdFundDtl.setFee(BigDecimal.ZERO); // TODO: 2016/5/19 银行手续费 需要计算
                // 无用参数
                gsrtBankHfTdFundDtl.setBondc(BigDecimal.ZERO);
                gsrtBankHfTdFundDtl.setBondd(BigDecimal.ZERO);
                gsrtBankHfTdFundDtl.setFeec(BigDecimal.ZERO);
                gsrtBankHfTdFundDtl.setFeed(BigDecimal.ZERO);
                gsrtBankHfTdFundDtl.setEveunfrozenmoney(BigDecimal.ZERO);
                fundDtlList.add(gsrtBankHfTdFundDtl);
            }
        }
        return fundDtlList;
    }

    private String getFundDtlSql(String date) {
        StringBuilder sb = new StringBuilder();
        sb.append(" select o.mer_cust_id, o.platid, sum(o.chargeSum), sum(o.drawSum) from ");
        sb.append(" (select i.mer_cust_id, i.platid, sum(0) as chargeSum, sum(d.mer_txn_amt) as drawSum ");
        sb.append(" from GSRT_BANK_CUST_SUPERVISE_INFO i ");
        sb.append(" left join gsrt_account_draw d ");
        sb.append(" on i.mer_cust_id = d.mer_cust_id ");
        sb.append(" where  to_char(d.created_ts, 'YYYYMMDD') = '").append(date).append("' ");
        sb.append(" group by i.mer_cust_id, i.platid ");
        sb.append(" union all ");
        sb.append(" select i.mer_cust_id, i.platid, sum(r.mer_txn_amt) as chargeSum, sum(0) as drawSum ");
        sb.append(" from GSRT_BANK_CUST_SUPERVISE_INFO i ");
        sb.append(" left join gsrt_account_recharge r ");
        sb.append(" on i.mer_cust_id = r.mer_cust_id ");
        sb.append(" where  to_char(r.created_ts, 'YYYYMMDD') = '").append(date).append("' ");
        sb.append(" group by i.mer_cust_id, i.platid) o ");
        sb.append(" group by o.mer_cust_id, o.platid ");
        return sb.toString();
    }
}
